package mysql

import (
	"bluebell-backend/common"
	"bluebell-backend/model/po"
)

func InsertCategory(category *po.Category) (int, error) {
	err := db.Create(category).Error
	if err != nil {
		return common.ERROR, err
	}
	return common.SUCCESS, nil
}

func DeleteCateGory(id int64) (int, error) {
	p := new(po.Category)
	err := db.Where("id = ?", id).Delete(p).Error
	if err != nil {
		return common.ERROR, err
	}
	return common.SUCCESS, nil
}

func UpdateCateGory(category *po.Category) (int, error) {

	err := db.Model(category).Updates(&po.Category{
		Name: category.Name,
	}).Error
	if err != nil {
		return common.ERROR, err
	}
	return common.SUCCESS, nil
}

func GetOneCategoryByID(id int64) (*po.Category, error) {
	p := new(po.Category)
	err := db.Where("id = ?", id).First(p).Error
	if err != nil {
		return nil, err
	}
	return p, nil
}
